/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.admn;

import edu.uoc.tdp.pac4.beans.ClientVehicle;
import edu.uoc.tdp.pac4.beans.Solicitud;
import edu.uoc.tdp.pac4.comun.Enumerados;
import edu.uoc.tdp.pac4.comun.GestorAdministrativaInterface;
import edu.uoc.tdp.pac4.comun.PAC4Exception;
import edu.uoc.tdp.pac4.comun.Utils;
import edu.uoc.tdp.pac4.i18n.TDSLanguageUtils;
import java.awt.Color;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Locale;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author mirica
 */
public class GestionSolicitud extends JInternalFrame {
   
    static public enum Accion {ALTA, MODIFICACION, FINALIZACION};
    private Accion accion;            
    
    private GestorAdministrativaInterface conexion;
    private final String URL = "localhost";
    private final String JNDI_NAME ="Serv_Administrativa";
    private final int PORT = 1099;
    
    private Solicitud solicitud;
    private String nif;
    private int numSolicitud;
    
    /**
     * Creates new form GestionSolicitud
     */
    public GestionSolicitud(GestorAdministrativaInterface conn) {
        if(conn!=null)
            conexion=conn;
        initComponents();
        
        conectarServerRMI();
        
        seleccionDeIdioma();
        inicializarControles();
        
        buttonGroup1.add(optAlta);
        buttonGroup1.add(optModificacion);
        optModificacion.setSelected(true);
        lblMensaje.setVisible(false);
        btnFinalizar.setVisible(false);
        
        setInicializarPantalla(false, null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        buttonGroup5 = new javax.swing.ButtonGroup();
        buttonGroup6 = new javax.swing.ButtonGroup();
        buttonGroup7 = new javax.swing.ButtonGroup();
        buttonGroup8 = new javax.swing.ButtonGroup();
        buttonGroup9 = new javax.swing.ButtonGroup();
        buttonGroup10 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        btnComprobar = new javax.swing.JButton();
        lblTituloSolicitud = new javax.swing.JLabel();
        lblBuscar = new javax.swing.JLabel();
        txtBuscar = new javax.swing.JTextField();
        lblMensaje = new javax.swing.JLabel();
        optAlta = new javax.swing.JRadioButton();
        optModificacion = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        lblFecFinalT = new javax.swing.JLabel();
        lblFecAltaT = new javax.swing.JLabel();
        lblEstadoT = new javax.swing.JLabel();
        lblEstado = new javax.swing.JLabel();
        lblNumSolT = new javax.swing.JLabel();
        lblNumSolicitud = new javax.swing.JLabel();
        lblFecAlta = new javax.swing.JLabel();
        lblPoliza = new javax.swing.JLabel();
        txtPoliza = new javax.swing.JTextField();
        lblAseguradora = new javax.swing.JLabel();
        txtAseguradora = new javax.swing.JTextField();
        lblComentarios = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtComentarios = new javax.swing.JTextArea();
        lblFecFin = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        btnCancelar = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        btnFinalizar = new javax.swing.JButton();

        jButton2.setText("jButton2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnComprobar.setText("COMPROBAR");
        btnComprobar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnComprobarActionPerformed(evt);
            }
        });

        lblTituloSolicitud.setFont(new java.awt.Font("Calibri", 3, 14)); // NOI18N
        lblTituloSolicitud.setForeground(new java.awt.Color(51, 51, 255));
        lblTituloSolicitud.setText("SOLICITUD");

        lblBuscar.setText("CAMPO");

        lblMensaje.setText("[MENSAJE]");

        optAlta.setText("ALTA");
        optAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optAltaActionPerformed(evt);
            }
        });

        optModificacion.setText("MODIFICACION");
        optModificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optModificacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMensaje)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(optModificacion)
                                .addGap(18, 18, 18)
                                .addComponent(optAlta)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTituloSolicitud)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lblBuscar)
                                .addGap(43, 43, 43)
                                .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnComprobar)))
                        .addGap(0, 129, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(lblTituloSolicitud)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(optAlta)
                    .addComponent(optModificacion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblBuscar)
                    .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnComprobar))
                .addGap(9, 9, 9)
                .addComponent(lblMensaje)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblFecFinalT.setText("FECHA_FINAL");

        lblFecAltaT.setText("FECHA_ALTA");

        lblEstadoT.setText("ESTADO");

        lblEstado.setText("--");

        lblNumSolT.setText("NUM_SOLICUTUD");

        lblNumSolicitud.setText("--");

        lblFecAlta.setText("--");

        lblPoliza.setText("NUM_POLIZA");

        lblAseguradora.setText("ASEGURADORA");

        lblComentarios.setText("COMENTARIOS");

        txtComentarios.setColumns(20);
        txtComentarios.setRows(5);
        jScrollPane1.setViewportView(txtComentarios);

        lblFecFin.setText("--");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblComentarios)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblFecAltaT, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblPoliza))
                                .addGap(27, 27, 27)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtPoliza)
                                    .addComponent(lblFecAlta, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(lblNumSolT)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblNumSolicitud)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblEstadoT)
                            .addComponent(lblAseguradora)
                            .addComponent(lblFecFinalT))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtAseguradora)
                            .addComponent(lblFecFin, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                            .addComponent(lblEstado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblNumSolT)
                        .addComponent(lblNumSolicitud))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblEstadoT)
                        .addComponent(lblEstado)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFecAltaT)
                    .addComponent(lblFecAlta)
                    .addComponent(lblFecFinalT)
                    .addComponent(lblFecFin))
                .addGap(8, 8, 8)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPoliza)
                    .addComponent(txtPoliza, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAseguradora)
                    .addComponent(txtAseguradora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblComentarios)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                .addContainerGap())
        );

        btnCancelar.setText("CANCELAR");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAceptar.setText("ACEPTAR");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnFinalizar.setText("FINALIZAR");
        btnFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFinalizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnFinalizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnAceptar)
                    .addComponent(btnFinalizar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnComprobarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnComprobarActionPerformed
        if(optModificacion.isSelected()) {
            if(validarNumSolicitud(txtBuscar.getText())) {
                numSolicitud = Integer.parseInt(txtBuscar.getText());
                try {
                    solicitud = conexion.obtenerSolicitud(numSolicitud);
                    if(solicitud != null) {
                        accion = Accion.MODIFICACION;
                        UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_EXISTE_MODIFICACION"), true, false);
                        if(solicitud.getIdSolicitudEstadoTipo() != Enumerados.SolicitudEstadoTipo.FINALIZADA.getEstado())
                            setInicializarPantalla(true, accion);
                        else
                            setInicializarPantalla(false, accion);
                        getCargarSolicitud(solicitud);
                    } 
                    else {
                        setInicializarPantalla(false, null);
                        UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_NO_EXISTE_SOLICITUD"), true, true);
                        lblNumSolicitud.setText("--");
                        nif = "";
                        numSolicitud = 0;
                    }
                } catch (RemoteException ex) {
                    Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
                } catch (PAC4Exception ex) {
                    Utils.shownError(TDSLanguageUtils.getMessage("Admin.Error"));
                }
            }
            else {
                UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Solicitud.NUM_SOLICITUD_BUSCAR_OBLIGATORIO_INCORRECTO"), true, true);
                setInicializarPantalla(false, null);
                solicitud = null;
                lblNumSolicitud.setText("--");                
            }
        }
        else if (optAlta.isSelected()) {
            nif = txtBuscar.getText();
            if (UtilsAdmin.validadoNIF(txtBuscar.getText()))
            {
                try {
                    ClientVehicle clientVehicle = conexion.obtenerCliente(nif); 
                    if(clientVehicle == null) {
                        setInicializarPantalla(false, null);
                        UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_NO_EXISTE_CLIENTE"), true, true);
                        lblNumSolicitud.setText("--");
                        nif = "";
                    }
                    else {
                        accion = Accion.ALTA;
                        UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_NO_EXISTE_ALTA"), true, false);
                        setInicializarPantalla(true, accion);
                    }
                } catch (RemoteException ex) {
                    Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
                } catch (PAC4Exception ex) {
                    Utils.shownError(TDSLanguageUtils.getMessage("Admin.Error"));
                }
            }
            else {
                UtilsAdmin.setMensaje(lblMensaje, TDSLanguageUtils.getMessage("Admin.Solicitud.NIF_BUSCAR_OBLIGATORIO_INCORRECTO"), true, true);
                setInicializarPantalla(false, null);
                solicitud = null;
                lblNumSolicitud.setText("--");
            } 
        }

    }//GEN-LAST:event_btnComprobarActionPerformed
    
    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        if(validarDatosSolicitud()){
            try {
                if(accion == Accion.ALTA){
                    cargarDatosSolicitud(Accion.ALTA);
                    solicitud = conexion.insertarSolicitud(solicitud);   
                    JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Admin.Solicitud.Solicitud_insertada"), TDSLanguageUtils.getMessage("Admin.Aviso"), JOptionPane.INFORMATION_MESSAGE);
                }
                else if(accion == Accion.MODIFICACION){
                    cargarDatosSolicitud(Accion.MODIFICACION);
                    conexion.actualizarSolicitud(solicitud);
                    JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Admin.Solicitud.Solicitud_modificada"), TDSLanguageUtils.getMessage("Admin.Aviso"), JOptionPane.INFORMATION_MESSAGE);
                }
                getCargarSolicitud(solicitud);
                optModificacion.setSelected(true);
            } catch (RemoteException ex) {
                Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
            } catch (PAC4Exception ex) {
                Utils.shownError(TDSLanguageUtils.getMessage("Admin.Error"));
            } 
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void optModificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optModificacionActionPerformed
        lblBuscar.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Num_Solicitud"));
        lblMensaje.setText("");
        txtBuscar.setText("");
        setInicializarPantalla(false, null);
        numSolicitud = 0;
        nif = "";
    }//GEN-LAST:event_optModificacionActionPerformed

    private void optAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optAltaActionPerformed
        lblBuscar.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.DNI"));
        lblMensaje.setText("");
        txtBuscar.setText("");
        setInicializarPantalla(false, null);
        numSolicitud = 0;
        nif = "";
    }//GEN-LAST:event_optAltaActionPerformed

    private void btnFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFinalizarActionPerformed
        if(validarDatosSolicitud()){
            try {
                cargarDatosSolicitud(accion.FINALIZACION);
                solicitud = conexion.actualizarSolicitud(solicitud);
                JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Admin.Solicitud.Solicitud_finalizada"), TDSLanguageUtils.getMessage("Admin.Aviso"), JOptionPane.INFORMATION_MESSAGE);
                setInicializarPantalla(false, accion);
                getCargarSolicitud(solicitud);
            } catch (RemoteException ex) {
                Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
            } catch (PAC4Exception ex) {
                Utils.shownError(TDSLanguageUtils.getMessage("Admin.Error"));
            }     
        }
    }//GEN-LAST:event_btnFinalizarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GestionSolicitud.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GestionSolicitud.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GestionSolicitud.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GestionSolicitud.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GestionSolicitud(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnComprobar;
    private javax.swing.JButton btnFinalizar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup10;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.ButtonGroup buttonGroup5;
    private javax.swing.ButtonGroup buttonGroup6;
    private javax.swing.ButtonGroup buttonGroup7;
    private javax.swing.ButtonGroup buttonGroup8;
    private javax.swing.ButtonGroup buttonGroup9;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblAseguradora;
    private javax.swing.JLabel lblBuscar;
    private javax.swing.JLabel lblComentarios;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JLabel lblEstadoT;
    private javax.swing.JLabel lblFecAlta;
    private javax.swing.JLabel lblFecAltaT;
    private javax.swing.JLabel lblFecFin;
    private javax.swing.JLabel lblFecFinalT;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JLabel lblNumSolT;
    private javax.swing.JLabel lblNumSolicitud;
    private javax.swing.JLabel lblPoliza;
    private javax.swing.JLabel lblTituloSolicitud;
    private javax.swing.JRadioButton optAlta;
    private javax.swing.JRadioButton optModificacion;
    private javax.swing.JTextField txtAseguradora;
    private javax.swing.JTextField txtBuscar;
    private javax.swing.JTextArea txtComentarios;
    private javax.swing.JTextField txtPoliza;
    // End of variables declaration//GEN-END:variables

   private void seleccionDeIdioma() {
        Locale localLocale = new Locale("es", "ES");
        TDSLanguageUtils.setDefaultLanguage("i18n/messages");
    }
   
    private void inicializarControles() {
        lblTituloSolicitud.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.TituloBusqueda"));
        optModificacion.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Modificacion"));
        optAlta.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Alta"));
        btnComprobar.setText(TDSLanguageUtils.getMessage("Admin.BotonBuscar"));
        lblNumSolT.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Num_Solicitud"));
        lblEstadoT.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Estado"));
        lblFecAltaT.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.FechaAlta"));
        lblFecFinalT.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.FechaFin"));
        lblPoliza.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Poliza"));
        lblAseguradora.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Aseguradora"));
        lblComentarios.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Comentarios"));
        btnFinalizar.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Finalizar"));
        btnAceptar.setText(TDSLanguageUtils.getMessage("Admin.Aceptar"));
        btnCancelar.setText(TDSLanguageUtils.getMessage("Admin.Cancelar"));
    }
    
    private void conectarServerRMI() {
        try {
            Registry registry = LocateRegistry.getRegistry(URL,PORT);
            this.conexion = (GestorAdministrativaInterface)registry.lookup(JNDI_NAME);
        } catch (NotBoundException e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
            System.exit(0);
        } catch (RemoteException e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Admin.ErrorRMI"));
            System.exit(0);
        } catch (Exception e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Aplication.ExcpGeneral"));
            System.exit(0);
        }    
    }
        
    private void setInicializarPantalla(boolean isEditable, Accion accion) {
        txtAseguradora.setEnabled(isEditable);
        txtPoliza.setEnabled(isEditable);
        txtComentarios.setEnabled(isEditable);
        txtAseguradora.setText("");
        txtPoliza.setText("");
        txtComentarios.setText("");
        lblNumSolicitud.setText("--");
        lblFecAlta.setText("--");
        lblFecFin.setText("--");
        lblEstado.setText("--");
        lblEstado.setForeground(Color.black);
        btnFinalizar.setVisible(isEditable);
        
        //Inicializar campos de filtro según seleccionado alta o modificación
        if(optModificacion.isSelected())
            lblBuscar.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.Num_Solicitud"));
        else if(optAlta.isSelected())
            lblBuscar.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.DNI"));
    }
    
    private void getCargarSolicitud(Solicitud solicitud) throws PAC4Exception {
        try {
            lblNumSolicitud.setText(Integer.toString(solicitud.getNumSol()));
            CargarEstado(solicitud);
            lblFecAlta.setText(solicitud.getDataAlta().toString());
            if(solicitud.getIdSolicitudEstadoTipo() == Enumerados.SolicitudEstadoTipo.FINALIZADA.getEstado())
                lblFecFin.setText(solicitud.getDataFinalitzacio().toString());
            else
                lblFecFin.setText("--");
            txtAseguradora.setText(solicitud.getAsseguradora());
            txtComentarios.setText(solicitud.getComentaris());
            txtPoliza.setText(solicitud.getNumPoliza());
        } catch (Exception e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_ERROR_CARGAR_SOLICITUD"));
        }  
    }
    
    private void CargarEstado(Solicitud solicitud) {
        int idEstado = solicitud.getIdSolicitudEstadoTipo();
        
        if(idEstado == Enumerados.SolicitudEstadoTipo.ACEPTADA.getEstado()) {
            lblEstado.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_ESTADO_ACEPTADA"));
            lblEstado.setForeground(Color.green);
            btnFinalizar.setVisible(true);
        }
        else if (idEstado == Enumerados.SolicitudEstadoTipo.EN_ESPERA_ACEPTACION.getEstado()) {
            lblEstado.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_ESTADO_EN_ESPERA_ACEPTACION"));
            lblEstado.setForeground(Color.green);            
            btnFinalizar.setVisible(true);
        }
        else if (idEstado == Enumerados.SolicitudEstadoTipo.DENEGADA.getEstado()) {
            lblEstado.setText( TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_ESTADO_DENEGADA"));
            lblEstado.setForeground(Color.red);
            btnFinalizar.setVisible(true);
        }
        else if (idEstado == Enumerados.SolicitudEstadoTipo.FINALIZADA.getEstado()) {
            lblEstado.setText(TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_ESTADO_FINALIZADA"));
            lblEstado.setForeground(Color.red);
        }
    }
    
    private boolean validarDatosSolicitud() {
        //Todos los campos son obligatorios
        if(txtPoliza.getText().isEmpty() || txtAseguradora.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Admin.Solicitud.CAMPOS_OBLIGATORIOS"), TDSLanguageUtils.getMessage("Admin.Aviso"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        return true;
    }
    
    private void cargarDatosSolicitud(Accion accion) throws PAC4Exception {
        try {
            if(solicitud == null)
                solicitud = new Solicitud();

            solicitud.setAsseguradora(txtAseguradora.getText());
            solicitud.setClient(nif);
            solicitud.setComentaris(txtComentarios.getText());
            solicitud.setNumPoliza(txtPoliza.getText());
            
            if(accion == accion.ALTA)
                solicitud.setIdSolicitudEstadoTipo(Enumerados.SolicitudEstadoTipo.EN_ESPERA_ACEPTACION.getEstado());
            else if(accion == accion.FINALIZACION)
                solicitud.setIdSolicitudEstadoTipo(Enumerados.SolicitudEstadoTipo.FINALIZADA.getEstado());
        } catch (Exception e) {
            Utils.shownError(TDSLanguageUtils.getMessage("Admin.Solicitud.SOLICITUD_ERROR_CARGAR_SOLICITUD"));
        } 
    }
    
    private boolean validarNumSolicitud(String numSolicitud) {
        if(numSolicitud.isEmpty())
            return false;        
        else {
            try {
                int tempNumSolicitud = Integer.parseInt(numSolicitud);
            } catch (Exception ex) {
                return false; 
            }
        }
        
        return true;
    }
}
